package com.thinkgem.jeesite.modules.fantasy.service.impl;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.thinkgem.jeesite.modules.fantasy.common.constant.Constants;
import com.thinkgem.jeesite.modules.fantasy.entity.City;
import com.thinkgem.jeesite.modules.fantasy.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.fantasy.dao.PropertyDao;
import com.thinkgem.jeesite.modules.fantasy.dao.FantasyUserDao;
import com.thinkgem.jeesite.modules.fantasy.dao.FantasyUserDetailDao;
import com.thinkgem.jeesite.modules.fantasy.entity.Property;
import com.thinkgem.jeesite.modules.fantasy.entity.FantasyUser;
import com.thinkgem.jeesite.modules.fantasy.entity.FantasyUserDetail;

@Service
public class FantasyUserService extends CrudService<FantasyUserDao, FantasyUser> {
	@Autowired
	private FantasyUserDao userDao;
	@Autowired
	private PropertyDao propertyDao;
	@Autowired
	private FantasyUserDetailDao userDetailDao;

	@Autowired
	private RedisUtil redisUtil;

	/**
	 * 检查用户名是否存在
	 * @param username
	 * @return
	 */
	public String checkUser(String username) {
		String flag = "-99";
		logger.debug(username);
		int result = userDao.checkUser(username);
		logger.debug("result == " + result);
		if (result > 0) {
			flag = "user_exist";
		} else {
			flag = "user_available";
		}
		return flag;
	}
	/**
	 * 检查昵称是否存在
	 * @param nickname
	 * @return
	 */
	public String checkNickname(String nickname) {
		String flag = "-99";
		logger.debug(nickname);
		int result = userDao.checkNickname(nickname);
		logger.debug("result == " + result);
		if (result > 0) {
			flag = "nick_exist";
		} else {
			flag = "nick_available";
		}
		return flag;
	}
	/**
	 * 检查邮箱是否可用
	 * @param email
	 * @return
	 */
	public String checkEmail(String email) {
		String flag = "-99";
		logger.debug(email);
		int result = userDao.checkEmail(email);
		logger.debug("result == " + result);
		if (result > 0) {
			flag = "email_exist";
		} else {
			flag = "email_available";
		}
		return flag;
	}
	/**
	 * 注册账户信息
	 * @param user
	 * @return
	 */
	public String register(FantasyUser user) {
		String flag = "";
		//检查用户名是否存在
		if ("user_exist".equals(this.checkUser(user.getUsername()))) {
			flag = "user_exist";
		} else if ("email_exist".equals(this.checkEmail(user.getEmail()))) {
			flag = "email_exist";
		} else if ("nick_exist".equals(this.checkNickname(user.getNickname()))) {
			flag = "nick_exist";
		} else {
			//向用户表中插入用户信息
			if (userDao.register(user) == 1) {
				//发送邮件
				flag = this.sendEmail(user, "register");
			} else {
				flag = "register_no";
			}
		}
		
		return flag;
	}
	/**
	 * 重置密码
	 * @param username
	 * @return
	 */
	public String emailAuthForGetPassword(String username) {
		String emailAuthFlag = "";
		logger.debug("重置密码。");
		emailAuthFlag = "getpassword";
		return emailAuthFlag;
	}
	/**
	 * 用户登陆
	 * @param user
	 * @return
	 */
	public FantasyUser login(FantasyUser user) {
		user = userDao.login(user);
		return user;
	}
	
	public FantasyUser selectUser(String username) {
		//获取用户对象
		return userDao.selectUser(username);
	}
	
	public String emailAuthForRegister(String username) {
		return null;
	}
	
	public String sendEmail(FantasyUser user, String authFor) {
		return null;
	}
	/**
	 * 根据用户id查询属性
	 * @param userid
	 * @return
	 */
	public Property getPropByUserId(String userid) {
		return propertyDao.getPropByUserId(userid);
	}
	/**
	 * 更新属性值
	 * @param property
	 * @return
	 */
	public Integer modifyProperty(Property property) {
		return propertyDao.modifyProperty(property);
	}
	/**
	 * 更新属性值上限
	 * @param property
	 * @return
	 */
	public Integer upgradePropertyTop(Property property) {
		return propertyDao.upgradePropertyTop(property);
	}
	/**
	 * 更新属性值
	 * 参数：用户id，属性名，属性值
	 * @param userid
	 * @param propName
	 * @param propValue
	 * @return
	 */
	public Integer modifyProperty(String userid, String propName, Integer propValue) {
		Property property = new Property();
		try {
			Class<?> propClass = (Class<?>) property.getClass();
			Field field =  propClass.getDeclaredField("userid");
			field.setAccessible(true); //设置这些属性是可以访问的
			field.set(property, userid);
			field = propClass.getDeclaredField(propName);
			field.setAccessible(true); //设置这些属性是可以访问的
			field.set(property, propValue);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return propertyDao.modifyProperty(property);
	}
	/**
	 * 获取属性值
	 * @param userid
	 * @param propName
	 * @return
	 */
	public Integer getPropertyByName(String userid, String propName) {
		Property property = propertyDao.getPropByUserId(userid);
		Integer propValue = null;
		try {
			Class<?> propClass = (Class<?>) property.getClass();
			Field field =  propClass.getDeclaredField("userid");
			field.setAccessible(true); //设置这些属性是可以访问的
			field.set(property, userid);
			field = propClass.getDeclaredField(propName);
			field.setAccessible(true); //设置这些属性是可以访问的
			propValue = (Integer) field.get(property);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return propValue;
	}
	
	public Map<String, Integer> getPropMapByName(String userid) {
		Property property = propertyDao.getPropByUserId(userid);
		Map<String, Integer> propMap = new HashMap<String, Integer>();
		try {
			Class<?> propClass = (Class<?>) property.getClass();
			Field field = null;
			Field[] fields = propClass.getDeclaredFields();
			for (int i=0; i<fields.length; i++) {
				String propName = fields[i].getName();
				//System.out.println(propName);
				field = propClass.getDeclaredField(propName);
				field.setAccessible(true); //设置这些属性是可以访问的
				//System.out.println(field.get(property));
				propMap.put(propName, (Integer) field.get(property));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return propMap;
	}
	/**
	 * 根据用户userid查询对应属性
	 * @param userid
	 * @return
	 */
	public FantasyUserDetail getDetailByUserId(String userid) {
		return userDetailDao.getDetailByUserId(userid);
	}
	/**
	 * 根据用户传递的属性修改对应的属性值
	 * @param userDetail
	 * @return
	 */
	@Transactional(readOnly = false)
	public Integer modifyUserDetail(FantasyUserDetail userDetail) {
		
		return userDetailDao.modifyUserDetail(userDetail);
	}
	/**
	 * 修改昵称
	 * @param userid
	 * @param nickname
	 * @return
	 */
	public Integer changeUserNickname(String userid, String nickname) {
		FantasyUser user = new FantasyUser();
		user.setId(userid);
		user.setNickname(nickname);
		return userDao.updateNickname(user);
	}
	/**
	 * 修改称号
	 * @param id
	 * @param chenghao
	 * @return
	 */
	public Integer changeUserChenghao(String id, String chenghao) {
		FantasyUserDetail detail = new FantasyUserDetail();
		detail.setId(id);
		detail.setChenghao(chenghao);
		return modifyUserDetail(detail);
	}
	/**
	 * 修改上线时间
	 * @param id
	 * @param landingtime
	 * @param addTime
	 * @return
	 */
	@Transactional(readOnly = false)
	public Integer changeUserLandingtime(String id, Integer landingtime,
			Integer addTime) {
		FantasyUserDetail detail = new FantasyUserDetail();
		detail.setId(id);
		detail.setLandingtime(landingtime + addTime);
		return modifyUserDetail(detail);
	}
	/**
	 * 修改上线次数
	 * @param id
	 * @param logintimes
	 * @return
	 */
	@Transactional(readOnly = false)
	public Integer changeUserLogintimes(String id, Integer logintimes) {
		FantasyUserDetail detail = new FantasyUserDetail();
		detail.setId(id);
		detail.setLogintimes(logintimes + 1);
		return modifyUserDetail(detail);
	}
	/**
	 * 修改体力值
	 * @param id
	 * @param strength
	 * @param alter
	 * @return
	 */
	public Integer changeUserStrength(String id, Integer strength,
			Integer alter) {
		FantasyUserDetail detail = new FantasyUserDetail();
		detail.setId(id);
		detail.setStrength(strength + alter);
		return modifyUserDetail(detail);
	}
	/**
	 * 修改体力值上限
	 * @param id
	 * @param strengthtop
	 * @param alter
	 * @return
	 */
	public Integer changeUserStrengthtop(String id, Integer strengthtop,
			Integer alter) {
		FantasyUserDetail detail = new FantasyUserDetail();
		detail.setId(id);
		detail.setStrengthtop(strengthtop + alter);
		return modifyUserDetail(detail);
	}
	/**
	 * 修改当前经验值
	 * @param id
	 * @param exp
	 * @param alter
	 * @return
	 */
	public Integer changeUserExp(String id, Integer exp, Integer alter) {
		FantasyUserDetail detail = new FantasyUserDetail();
		detail.setId(id);
		detail.setExp(exp + alter);
		return modifyUserDetail(detail);
	}
	/**
	 * 修改下一级所需经验值
	 * @param id
	 * @param nextexp
	 * @param rate
	 * @return
	 */
	public Integer changeUserNextExp(String id, Integer nextexp, Double rate) {
		Double temp = nextexp * rate;
		nextexp = temp.intValue();
		FantasyUserDetail detail = new FantasyUserDetail();
		detail.setId(id);
		detail.setNextexp(nextexp);
		return modifyUserDetail(detail);
	}
	/**
	 * 修改等级
	 * @param id
	 * @param level
	 * @param alter
	 * @return
	 */
	public Integer changeUserLevel(String id, Integer level, Integer alter) {
		FantasyUserDetail detail = new FantasyUserDetail();
		detail.setId(id);
		detail.setLevel(level + alter);
		return modifyUserDetail(detail);
	}
	/**
	 * 修改钱数
	 * @param id
	 * @param money
	 * @param alter
	 * @return
	 */
	public Integer changeUserMoney(String id, Integer money, Integer alter) {
		FantasyUserDetail detail = new FantasyUserDetail();
		detail.setId(id);
		detail.setMoney(money + alter);
		return modifyUserDetail(detail);
	}

	/**
	 * 更新用户所在城市
	 * @param user
	 * @param moveTo
	 */
	public void moveUser(FantasyUser user, String moveTo) {

		//获取当前地点所属城市id
		String cityId = (String) redisUtil.getValue(Constants.REDIS_CITYMAP_ + moveTo);
		if (cityId != null) {
			Boolean isMember = redisUtil.isMember(Constants.REDIS_CITY_ + cityId, user.getId());
			if (isMember != true) {
				//登陆成功后，把用户保存到相关城市的set中
				redisUtil.addMember(Constants.REDIS_CITY_ + cityId, user.getId());
			}
		} else {
			//登陆成功后，把用户保存到相关城市的set中
			redisUtil.addMember(Constants.REDIS_CITY_ + cityId, user.getId());
		}
	}
}
